package com.khaled.protclass.server.common;

import java.util.ArrayList;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.khaled.protclass.model.bean.prediction.PredictionResult;

public class HTMLEmail
{
	private static HTMLEmail htmlEmail;

	private Configuration config;

	private Session session;

	private HTMLEmail()
	{
		this.config = Configuration.getInstance();

		final String username = this.config.get("mail.smtp.user");
		final String password = this.config.get("mail.smtp.password");
		// Get system properties
		Properties properties = System.getProperties();

		properties.put("mail.smtp.host", this.config.get("mail.smtp.host"));
		properties.put("mail.smtp.socketFactory.port", this.config.get("mail.smtp.port"));
		properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		properties.put("mail.smtp.auth", "true");
		properties.put("mail.smtp.port", this.config.get("mail.smtp.port"));

		properties.setProperty("mail.transport.protocol", "smtp");

		// Get the default Session object.
		this.session = Session.getDefaultInstance(properties, new javax.mail.Authenticator()
		{
			protected PasswordAuthentication getPasswordAuthentication()
			{
				return new PasswordAuthentication(username, password);
			}
		});
	}

	public static HTMLEmail getInstance()
	{
		if (htmlEmail == null)
		{
			htmlEmail = new HTMLEmail();
		}

		return htmlEmail;
	}

	public void send(ArrayList<PredictionResult> predictionResult, String email) throws MessagingException
	{
		// Create a default MimeMessage object.

		MimeMessage message = new MimeMessage(session);

		// Set From: header field of the header.
		message.setFrom(new InternetAddress(this.config.get("mail.smtp.from")));

		// Set To: header field of the header.
		message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));

		// Set Subject: header field
		message.setSubject(this.config.get("server.name") + " Results.");

		// Send the actual HTML message, as big as you like
		String content = formatContent(this.config.get("server.name"), predictionResult);
		message.setContent(content, "text/html");

		// Send message

		Transport.send(message);

		// Transport transport = session.getTransport("smtp");
		// transport.connect(this.config.get("mail.smtp.user"),
		// this.config.get("mail.smtp.password"));
		// transport.sendMessage(message, message.getAllRecipients());
		// transport.close();

	}

	public void sendError(ArrayList<String> errorMessages, String email) throws MessagingException
	{
		// Create a default MimeMessage object.

		MimeMessage message = new MimeMessage(session);

		// Set From: header field of the header.
		message.setFrom(new InternetAddress(this.config.get("mail.smtp.from")));

		// Set To: header field of the header.
		message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));

		// Set Subject: header field
		message.setSubject(this.config.get("server.name") + " Results.");

		// Send the actual HTML message, as big as you like
		String content = formatError(this.config.get("server.name"), errorMessages);
		message.setContent(content, "text/html");

		// Send message

		Transport.send(message);

		// Transport transport = session.getTransport("smtp");
		// transport.connect(this.config.get("mail.smtp.user"),
		// this.config.get("mail.smtp.password"));
		// transport.sendMessage(message, message.getAllRecipients());
		// transport.close();

	}

	private String formatContent(String serverName, ArrayList<PredictionResult> predictionResult)
	{
		StringBuffer content = new StringBuffer("Thank you for using " + serverName + ".\n");
		content.append("<BR>Prediction Results in the table below:\n");
		content.append("<BR><TABLE border=\"2\">");
		content.append("<TR><TH align=\"center\">Protein Name</TH><TH align=\"center\">Prediction result</TH><TH align=\"center\">Predicted Score</TH></TR>");

		for (PredictionResult res : predictionResult)
		{
			content.append("<TR><TD align=\"center\">");
			content.append(res.getProtienID());

			content.append("</TD><TD align=\"center\">");
			content.append(res.getPredictionClass());

			content.append("</TD><TD align=\"center\">");
			content.append(res.getPredictionScore());

			content.append("</TD></TR>");
		}

		content.append("</TABLE>");

		return content.toString();
	}

	private String formatError(String serverName, ArrayList<String> errorMessage)
	{
		StringBuffer content = new StringBuffer("Thank you for using " + serverName + ".\n");
		content.append("<BR>Unfortunately, The ").append(serverName).append(" has encountered an internal error.<BR>");
		content.append("Error Details:<BR>");
		content.append("<ul>");
		for (String error : errorMessage)
		{
			content.append("<li>").append(error).append("</li>");
		}
		content.append("</ul>");

		return content.toString();
	}
}